package com.kaishengit.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.kaishengit.dao.AtMessageDao;
import com.kaishengit.dao.CommentDao;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.dao.UserDao;
import com.kaishengit.entity.AtMessage;
import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.Page;


public class MessageService {
	
	private MessageDao dao=new MessageDao();
	private UserDao userdao = new UserDao();
	private AtMessageDao adao = new AtMessageDao();
	private CommentDao cdao=new CommentDao();
	private UserDao udao=new UserDao();
	public void save(String projectid, String userid, String content, String type) {
		String date=DateUtil.getNow();
		String mid=PKutil.getPk();
		dao.saveInformation(mid,content,date,userid,projectid,type);
		String reg = "@.+\\s";
		Matcher m=Pattern.compile(reg).matcher(content);
		while(m.find()){ 
			String s = m.group();
			if(s!=null){
				String[] str = s.split(" ");
				for(String m2:str){
					m2=m2.substring(1);
					User user = userdao.findByName(m2);
					if(user!=null){
						adao.save(mid,user.getId(),projectid);	
					}
					
				}
			}
		};
	}
	/**
	 * 根据项目id获得所有的动态信息，并获得其分页
	 * */
	public Page<Message> findAllByProjectId(int pageNum,String projectid) {
		Page<Message> page=new Page<Message>();
		page.setPageNum(pageNum);
		page.setPageSize(3);
		page.setTotalCount(dao.findCount(projectid));
		page.getTotalCount();
		List<Message> lis= dao.findAllByProjectId(projectid,page.getOffest(),page.getPageSize());
		List<Message> list=new ArrayList<Message>();
		for(Message m:lis){
			String type=m.getType();
			if("document".equals(type)||"idea".equals(type)||"infor".equals(type)){
				List<Comment> clist=cdao.findByMessageId(m.getId());
				for(Comment com:clist){
					User user=udao.findById(com.getUserid());
					com.setUserid(user.getUsername());
				}
				m.setCommentlist(clist);
			}
			String time;
			try {
				time=DateUtil.getAccount(DateUtil.getNow(), m.getDate());
				m.setDate(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			list.add(m);
		}
		page.setList(list);
		return page;
	}
	
	/**
	 * 获得atMessage的分页
	 * */
	public Page<AtMessage> findByUserId(String id, String mepageNum) {
		Page<AtMessage> page = new Page<AtMessage>();
		int p =0;
		if(mepageNum != null){
			p=Integer.valueOf(mepageNum);
		}else{
			p=1;
		}
		page.setPageNum(p);
		page.setPageSize(8);
		page.setTotalCount(adao.findCount(id));
		page.getTotalCount();
		return adao.findByUserId(id,page);
		
	}
	public void delById(String id) {
		dao.delByDocumentId(id);
		
	}
	public void delByFileId(String id) {
		dao.delByFileId(id);
		
	}

}
